package com.monkey.core.domain.resource;

import com.monkey.core.plugin.domain.Status;
import com.monkey.core.plugin.domain.Treeable;
import com.monkey.utils.MonkeyUtils;
import org.hibernate.annotations.Formula;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@DiscriminatorValue("Menu")
public class Menu extends Resource implements Treeable<Long, Menu> {

    /**
     * 父路径
     */
    @Column(name = "parent_id")
    private Long parentId;

    @Column(name = "parent_ids")
    private String parentIds;

    private Integer weight;

    /**
     * 图标
     */
    private String icon;

    private String view;

    @Column(name = "route_id")
    private String routeId;

    /**
     * 是否有叶子节点
     */
    @Formula(value = "(select count(*) from sys_resource f_t where f_t.parent_id = id)")
    private boolean hasChildren;

    private boolean expand;

    @Enumerated(EnumType.STRING)
    private Status status;

    @Transient
    private List<Menu> children = new ArrayList<>();

    public Menu() {
    }

    @Override
    public Status getStatus() {
        return status;
    }

    @Override
    public void setStatus(Status status) {
        this.status = status;
    }

    public boolean isExpand() {
        return expand;
    }

    public void setExpand(boolean expand) {
        this.expand = expand;
    }

    public String getView() {
        return view;
    }

    public void setView(String view) {
        this.view = view;
    }

    public String getRouteId() {
        return routeId;
    }

    public void setRouteId(String routeId) {
        this.routeId = routeId;
    }

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    public String getParentIds() {
        return parentIds;
    }

    public void setParentIds(String parentIds) {
        this.parentIds = parentIds;
    }

    @Override
    public String makeSelfAsNewParentIds() {
        return getParentIds() + getId() + getSeparator();
    }

    @Override
    public String getSeparator() {
        return "/";
    }

    public Integer getWeight() {
        return weight;
    }

    public void setWeight(Integer weight) {
        this.weight = weight;
    }

    public String getIcon() {
        if (MonkeyUtils.isNotEmpty(icon)) {
            return icon;
        }
        if (isRoot()) {
            return getRootDefaultIcon();
        }
        if (isLeaf()) {
            return getLeafDefaultIcon();
        }
        return getBranchDefaultIcon();
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }


    @Override
    public boolean isRoot() {
        return getParentId() != null && getParentId() == 0;
    }


    @Override
    public boolean isLeaf() {
        if (isRoot()) {
            return false;
        }
        if (isHasChildren()) {
            return false;
        }

        return true;
    }

    public boolean isHasChildren() {
        return hasChildren;
    }

    public void setHasChildren(boolean hasChildren) {
        this.hasChildren = hasChildren;
    }

    /**
     * 根节点默认图标 如果没有默认 空即可
     */
    @Override
    public String getRootDefaultIcon() {
        return Treeable.DEFAULT_ROOT_ICON_CLS;
    }

    /**
     * 树枝节点默认图标 如果没有默认 空即可
     */
    @Override
    public String getBranchDefaultIcon() {
        return Treeable.DEFAULT_BRANCH_ICON_CLS;
    }

    /**
     * 树叶节点默认图标 如果没有默认 空即可
     */
    @Override
    public String getLeafDefaultIcon() {
        return Treeable.DEFAULT_LEAF_ICON_CLS;
    }

    @Override
    public List<Menu> getChildren() {
        return this.children;
    }

    @Override
    public void addChild(Menu child) {
        this.children.add(child);
    }
}
